﻿using AntdUI;
using MiniExcelLibs.Attributes;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZR.Model.Models.ProductionControl
{
    [SugarTable("Cks_PlcDataPosition")]
    public class CksPlcDataPosition : BaseEntity
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id {  get; set; }

        public int ParentId { get; set; }

        public int OrderNo {  get; set; }

        public int PlcId {  get; set; }

        public string WorkStationCode {  get; set; }

        [SugarColumn(IsIgnore =true)]
        public int TempId { get; set; }
        [SugarColumn(IsIgnore = true)]
        public int TempParentId { get; set; }
        /// <summary>
        /// 模块：1上位机 2AGV
        /// </summary>
        public int Module { get; set; }
        /// <summary>
        /// DB块主键ID
        /// </summary>
        public int BlockDbId { get; set; }
        /// <summary>
        /// DB块编号
        /// </summary>
        public int BlockDb {  get; set; }

        public int BlockLength {  get; set; }

        public decimal BytePosition {  get; set; }

        public int ByteLength {  get; set; }
        /// <summary>
        /// 解析类型 eg: bool\string\byte\...
        /// </summary>
        public string ParseType { get; set; }

        public string DefaultValue { get; set; } = string.Empty;
        /// <summary>
        /// 作用类型 EffectType:0 常规数据、1订阅数据、2 MES心跳、3 PLC心跳
        /// </summary>
        public int EffectType {  get; set; }

        public int ActionId { get; set; }

        /// <summary>
        /// 关联动作Id 
        /// </summary>
        public int LinkActionId { get; set; }
        /// <summary>
        /// 动作下的步骤编号 
        /// </summary>
        public int StepId { get; set; }

        public int LocationId {  get; set; }
        public int DataId { get; set; }

        public string Remark { get; set; } = string.Empty;
        /// <summary>
        /// 数据分类 1 常规读取 2 写入回复信号 ...(可扩展，eg:子篮码、底板码)
        /// 1XXX:读取某种编码 2XXX:写入某种编码
        /// </summary>
        public int DataKind { get; set; }

        public bool Nullity { get; set; }

        public string Description { get; set; } = "";

        [SqlSugar.SugarColumn(IsIgnore =true)]
        public string Address { 
            get
            {
                switch (ParseType)
                {
                    case "int":
                    case "int16":
                    case "word":
                        return $"DB{BlockDb}.DBW{BytePosition}";

                    case "byte":
                    case "char":
                        return $"DB{BlockDb}.DBB{BytePosition}";
                    case "real":
                    case "float":
                        return $"DB{BlockDb}.DBD{BytePosition}";
                    case "string":
                        return $"DB{BlockDb}.DBB{BytePosition}.{ByteLength}";
                    case "bool":
                        return $"DB{BlockDb}.DBX{BytePosition.ToString("0.0")  }";

                    default:
                        
                        throw new Exception("未实现地址转换");
                }

            }
        }

        [SugarColumn(ColumnName = "WriteFrom")]
        public int WriteFrom { get;set; }

        [SugarColumn(IsIgnore = true)]
        public string WriteFromText
        {
            get
            {



                return WriteFrom switch
                {
                    1 => "PC",
                    2 => "PLC",
                    3 => "混合",
                    _ => ""
                };
            }
        }
        [SugarColumn(IsIgnore = true)]
        public CellText WriteFromCellText
        {
            get
            {
                return new CellText(WriteFromText)
                {
                    //支持所有单元格控件
                    //支持所有单元格控件
                    DropDownItems = new ISelectItem[]
                         {
                             new AntdUI.SelectItem("PC",1),
                              new AntdUI.SelectItem("PLC",2),
                                new AntdUI.SelectItem("混合",3),
                         },
                    DropDownValueChanged = (value) =>
                    {
                        int badge = (value as AntdUI.SelectItem).Tag.ObjToInt();
                        WriteFrom = badge;
                        OnPropertyChanged("WriteFromCellText");
                    }
                };

            }

            //set => writeFromButton = value;
        }


    }
}
